/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import hibentities.Customer;
import hibentities.CustomerData;
import hibernatedaos.UserHibernateDao;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author piotrek
 */
@Controller
@RequestMapping(value="/hibernate")
public class UserHibernateController {

	private final UserHibernateDao userDao;

//	private @Autowired HttpServletRequest request;
	
	@Autowired
    public UserHibernateController( UserHibernateDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user", method=RequestMethod.GET)
    protected String showUserHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		model.addAttribute( "id", id );

		Customer customer = userDao.getUser( Long.parseLong(id) );

		model.addAttribute( "user", customer );
		model.addAttribute( "userd", customer.getCustomerData() );

		return "userh";
    }

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user/update", method=RequestMethod.GET)
    protected String updateUserHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		Customer c = userDao.getUser( Long.parseLong(id) );
		c.setLogin("zmieniony");
		c.getCustomerData().setStreet("inna");
		userDao.updateUser( c );

		return "success";
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user/delete", method=RequestMethod.GET)
    protected String deleteUserHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		userDao.deleteUser( Long.parseLong(id) );

		return "success";
	}

	/**
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/user/add")
	protected String saveUser( HttpServletRequest request,
            HttpServletResponse response){

		Customer c = new Customer();
//		c.setLogin( (String)request.getParameter("login") );
//		c.setPasswd( (String)request.getParameter("passwd") );

		c.setLogin( "login" );
		c.setPasswd( "haslo" );

		CustomerData cd = new CustomerData();
//		cd.setCity((String)request.getParameter("city"));
//		cd.setFlatNumber(Long.MIN_VALUE);
//		cd.setName((String)request.getParameter("name"));
//		cd.setPostal((String)request.getParameter("postal"));
//		cd.setStreet((String)request.getParameter("street"));
//		cd.setStreetNumber(Long.MIN_VALUE);
//		cd.setSurname((String)request.getParameter("surname"));

		cd.setCity( "miasto" );
		cd.setFlatNumber( 12 );
		cd.setName( "imie" );
		cd.setPostal("12-345");
		cd.setStreet( "ulica" );
		cd.setStreetNumber( 34 );
		cd.setSurname( "nazwisko");

		c.setCustomerData(cd);

		cd.setCustomer(c);

		userDao.addUser( c );

		return "success";

	}
	
}
